package com.cn.wanxi.service;

import com.cn.wanxi.IO.NewsIO;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.util.ReadNote;
import com.cn.wanxi.util.Tool;

import java.util.ArrayList;
import java.util.List;

public class NewsService {
    /**
     * 得到新闻信息
     *
     * @return
     */
    public List<NewsModel> getForNewsList() {
        NewsModel newsModel = new NewsModel();//只是为了获取实体类的数组
        String[] hrefForNews = newsModel.getHrefForNews();
        String[] titleForNews = newsModel.getTitleForNews();
        String[] createTimeForNews = newsModel.getCreateTimeForNews();
        return setNewsPageData(hrefForNews, titleForNews, createTimeForNews);
    }

    /**
     * 通過鏈接，标题，时间进行封装
     *
     * @param hrefs
     * @param titles
     * @param createTimes
     * @return
     */
    private List<NewsModel> setNewsPageData(String[] hrefs, String[] titles, String[] createTimes) {
        List<NewsModel> list = new ArrayList<>();
        for (int i = 0; i < hrefs.length; i++) {
            NewsModel model = new NewsModel();//这个是为了封装新闻信息到前台页面展示
            model.setHref(hrefs[i]);
            model.setTitle(titles[i]);
            model.setCreateTime(createTimes[i]);
            list.add(model);
        }
        return list;
    }

    /**
     * 得到网站首页的新闻动态
     *
     * @return
     */
    public List<NewsModel> getForHomeList() {

        NewsModel newsModel = new NewsModel();
        String titles[] = newsModel.getTitlesForHome();
        String contents[] = newsModel.getContentForHome();
        return setNewsDataForHomePage(titles, contents);
    }

    /**
     * 数据来自于IO流
     *
     * @return
     */
    public List<NewsModel> getListFromIO() {
        String path = Tool.newsPagePath.getPath();
        NewsIO newsIO = new NewsIO();
        newsIO.writeNote(path);//写入数据
        String string = ReadNote.read(path);//读取数据
        return getStringToArrayForNewsPage(string);//封装数据
    }

    /**
     * 解析读取出来的数据
     * 将字符串转换为需要的数组
     * 调用封装setNewsPageData数据的方法
     *
     * @param string
     * @return
     */
    public List<NewsModel> getStringToArrayForNewsPage(String string) {
        String temp[] = string.split(Tool.split.getPath());//将读取的数据分割成数组
        String href = temp[0];
        href = Tool.replace(href);
        String title = temp[1];
        title = Tool.replace(title);
        String createTime = temp[2];
        createTime = Tool.replace(createTime);
        String hrefs[] = href.split(",");
        String titles[] = title.split(",");
        String createTimes[] = createTime.split(",");
        return setNewsPageData(hrefs, titles, createTimes);
    }


    /**
     * 网站首页数据来源于记事本
     *
     * @return
     */
    public List<NewsModel> getNewsModelListForHomeFromIO() {
        String path = Tool.newsForHomePath.getPath();
//        1.写入记事本
        NewsIO newsIO = new NewsIO();
        newsIO.writeNoteForHome(path);
//        2.读取记事本
        String string = ReadNote.read(path);
//        3.封装数据
        return getStringToArrayForHomePage(string);
    }

    /**
     * 将字符串转换为数组并封装成数组对象
     *
     * @param string
     * @return
     */
    private List<NewsModel> getStringToArrayForHomePage(String string) {
        String temp[] = string.split(Tool.split.getPath());//将读取的数据分割成数组
        String title = temp[0];
        title = Tool.replace(title);
        String content = temp[1];
        content = Tool.replace(content);
        String titles[] = title.split(",");
        String contents[] = content.split(",");
        return setNewsDataForHomePage(titles, contents);
    }

    /**
     * 封装网站首页的新闻信息
     *
     * @param titles
     * @param contents
     * @return
     */
    private List<NewsModel> setNewsDataForHomePage(String[] titles, String[] contents) {
        List<NewsModel> list = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            NewsModel model = new NewsModel();
            model.setTitle(titles[i]);
            model.setContent(contents[i]);
            list.add(model);
        }
        return list;
    }

}
